/**
 * 新增资产变更
 */
var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.post('/',upload.array(), function(req, res, next) {
    let data = {
        isSuccess: false,
        data: null,
        msg: '资产资料修改失败！'
    };
    let equipmentid = _.trim(String(req.body.equipmentid));
    let depart = _.trim(String(req.body.depart));
    let workshop = _.trim(String(req.body.workshop));
    let departid = _.trim(String(req.body.departid));
    let workshopid = _.trim(String(req.body.workshopid));
    let user = _.trim(String(req.body.user));
    let contact = _.trim(String(req.body.contact));
    let location = _.trim(String(req.body.location));
    let desc = _.trim(String(req.body.desc));
    let recorderid = _.trim(String(req.body.recorderid));
    let recorddate = new Date().toLocaleDateString();

    let tableName = 'equipment_change_record';
    let colName = "`equipmentid`,`depart`,`departid`,`workshop`,`workshopid`,`user`,`contact`,`location`, `desc`,`recorderid`,`recorddate`";
    let colValue = "'" + equipmentid + "','" + depart + "','" + departid + "','" + workshop + "','" + workshopid
    + "','"+ user + "','" + contact + "','" + location
     + "','"+ desc + "','" + recorderid + "','" + recorddate + "'";

    let sql = "UPDATE equipmentinfo SET depart='" + depart + "', departid='" + departid + "',workshop='" + workshop 
    + "',workshopid='" + workshopid + "',user='" + user + "',contact='" + contact + "',location='" + location + "',note='" + 
    desc + "' WHERE id='" + equipmentid + "'"
    console.log(sql);

    db.insert(tableName, colName, colValue, function(err, rows) {
        if(err) {
            return res.json(data);
        } else {
            console.log(rows.affectedRows)
            if(rows.affectedRows > 0 ) {
                db.execute(sql, function(error, row) {
                    if(error) {
                        return res.json(data);
                    } else {
                        if(row.affectedRows > 0) {
                            data.isSuccess = true;
                            data.msg = "资产资料修改成功！";
                            return res.json(data);
                        }
                    }
                })
            } else {
                return res.json(data);
            }
        }
    })
})

module.exports = router;